package com.leyou.order.scheduled;

import com.leyou.client.item.ItemClient;
import com.leyou.order.dto.OrderVO;
import com.leyou.order.pojo.OrderDetail;
import com.leyou.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时更改订单状态为支付过期和失效的任务类
 */
@Component
@Slf4j
public class PayStatusScheduler {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ItemClient itemClient;

    // 支付过期时间:20分钟
    private static final long ORDER_OVERTIME = 1200000L;  //1200000L;

    // 订单失效时间:2小时
    private static final long ORDER_CANCELTIME = 7200000L;  //7200000L;

    // 定时任务执行间隔
    private static final long TASKTIME_INTERVAL = 60000L;

    // 定时任务失败后重试的间隔
    private static final long ORDER_RETRY_INTERVAL = 10000L;

    @Scheduled(fixedRate = TASKTIME_INTERVAL)
    public void checkOvertime(){
        while (true) {
            try {
                List<OrderVO> orderVOList = orderService.findAllOrder();
                orderVOList.forEach(orderVO -> {

                    // 如果订单创建时间超过20分钟
                    Long orderOverTime = orderVO.getCreateTime().getTime() + ORDER_OVERTIME;
                    Long nowTime = new Date().getTime();
                    if (orderVO.getStatus() == 1 && (orderOverTime <= nowTime) ){
                        // 更改订单状态为支付过期
                        orderService.updateOrderStatus(orderVO.getOrderId(), 6);
                        log.info("【检测支付过期订单】订单号:\"" + orderVO.getOrderId() + "支付超时!");

                        // 归还订单中商品的库存
                        List<OrderDetail> detailList = orderVO.getDetailList();
                        Map<Long, Integer> paramMap = new HashMap<>();
                        detailList.forEach(orderDetail -> {
                            paramMap.put(orderDetail.getSkuId(), orderDetail.getNum());
                        });
                        itemClient.addStock(paramMap);
                    }
                });
                break;
            } catch (Exception e) {
                log.error("【检测支付过期订单】检测支付过期订单服务失败，" + ORDER_RETRY_INTERVAL + "后重试!");
                //10秒后重试
                try {
                    Thread.sleep(ORDER_RETRY_INTERVAL);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    @Scheduled(fixedRate = TASKTIME_INTERVAL)
    public void checkOrderCancel(){
        while(true) {
            try {
                List<OrderVO> orderVOList = orderService.findAllOrder();
                orderVOList.forEach(orderVO -> {
                    if (orderVO.getStatus() == 6 && (orderVO.getCreateTime().getTime() + ORDER_CANCELTIME) <= new Date().getTime()) {
                        // 更改订单状态为支付过期
                        orderService.updateOrderStatus(orderVO.getOrderId(), 7);
                        log.info("【检测失效订单】订单号:" + orderVO.getOrderId() + "订单失效!");
                    }
                });
                break;
            } catch (Exception e) {
                log.error("【检测失效订单】检测失效订单服务失败，" + ORDER_RETRY_INTERVAL + "后重试!");
                //10秒后重试
                try {
                    Thread.sleep(ORDER_RETRY_INTERVAL);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


}
